package org.ruiyichen.guizhou.agr.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.ui.listener.OnRecyclerItemClick;
import org.ruiyichen.guizhou.agr.utils.ListUtil;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


/**
 * 类描述：家庭成员
 *
 * @version 1.0
 */
public class IrrigatedAreaRecyclerAdapter extends RecyclerView.Adapter<IrrigatedAreaRecyclerAdapter.BViewHolder> {

    private List<? extends IIrrigatedAreaFilter> mDatas;
    private Context mContext;
    private LayoutInflater inflater;

    private OnRecyclerItemClick onRecyclerItemClick;


    public IrrigatedAreaRecyclerAdapter(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmDatas(List<? extends IIrrigatedAreaFilter> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    public void setOnRecyclerItemClick(OnRecyclerItemClick onRecyclerItemClick) {
        this.onRecyclerItemClick = onRecyclerItemClick;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(BViewHolder holder, final int position) {
        holder.ivIrrigatedControl.setSelected(mDatas.get(position).getIIsDelete());
        holder.tvIrrigatedControlListDesc.setText(mDatas.get(position).getIIrrigatedAreaDesc());
        holder.ivIrrigatedControlNext.setVisibility(mDatas.get(position).getIIsDelete() ? View.GONE : View.VISIBLE);
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public BViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.df_item_irrigated_area, parent, false);
        BViewHolder holder = new BViewHolder(view);
        return holder;
    }

    public boolean isLastPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position == mDatas.size() - 1;
    }

    @Override
    public int getItemCount() {
        if (mDatas == null) return 0;
        return mDatas.size();
    }

    /**
     * 设置编辑状态
     *
     * @param isEdit
     */
    public void setIsEdit(boolean isEdit) {
        if (ListUtil.isEmpty(mDatas)) {
            return;
        }
        for (int i = 0; i < mDatas.size(); i++) {
            mDatas.get(i).setIIsEdit(isEdit);
        }
        notifyDataSetChanged();
    }


    /**
     * 协办员
     */
    class BViewHolder extends RecyclerView.ViewHolder {

        ImageView
                ivIrrigatedControl,
                ivIrrigatedControlNext;

        TextView
                tvIrrigatedControlListDesc;

        public BViewHolder(final View view) {
            super(view);
            ivIrrigatedControl = (ImageView) view.findViewById(R.id.iv_irrigated_control);
            ivIrrigatedControlNext = (ImageView) view.findViewById(R.id.iv_irrigated_control_next);
            tvIrrigatedControlListDesc = (TextView) view.findViewById(R.id.iv_irrigated_control_list_desc);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRecyclerItemClick != null) {
                        onRecyclerItemClick.onItemClick(v, getLayoutPosition());
                    }
                }
            });
        }
    }
}
